/**
 * 
 */
package com.tvshows.front.listener.command.interfaces;

import org.apache.log4j.Logger;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

import com.tvshows.front.listener.command.AddLinkCommand;
import com.tvshows.front.listener.command.DeleteRecapCommand;
import com.tvshows.front.listener.command.RefreshFavoritesCommand;
import com.tvshows.front.ui.recap.UpsideCompositeRecapUI;
import com.tvshows.front.view.recap.UpsideCompositeRecapView;

/**
 * @author Tangi Midy
 */
public class RecapCommandListener
    implements Listener
{

    /**
     * The {@link RecapCommand}
     */
    public RecapCommand command = null;

    /**
     * The {@link UpsideCompositeRecapView}
     */
    public UpsideCompositeRecapUI upsideComposite;

    /**
     * Logger of the class
     */
    private static Logger logger = Logger.getLogger( RecapCommandListener.class );

    /**
     * Constructeur
     * 
     * @param upsideComposite the {@link UpsideCompositeRecapView}
     */
    public RecapCommandListener( UpsideCompositeRecapUI upsideComposite )
    {
        this.upsideComposite = upsideComposite;
    }

    @Override
    public void handleEvent( Event e )
    {

        // Add live link
        if ( e.widget == upsideComposite.getAddLive() )
        {
            command = new AddLinkCommand( upsideComposite );
        }

        // Add show in live
        else if ( e.widget == upsideComposite.getAddShowInLive() )
        {
            upsideComposite.getAddLive().setEnabled(!upsideComposite.getAddShowInLive().getText().isEmpty() );
        }

        // Delete show from the table
        else if ( e.widget == upsideComposite.getDeleteShowRecap() )
        {
            command = new DeleteRecapCommand();
        }

        // Refresh the table
        else if ( e.widget.equals( upsideComposite.getRefreshList() ) )
        {
            command = new RefreshFavoritesCommand();
        }

        // prevents crashes
        if ( command != null )
        {
            command.executeCommand();
            logger.info( "Commande " + command.getClass() + " exécutée " );
        }
        command = null;

    }

}
